-- basic dynamic msg_box -- alundaio class "msg_box_ui" (CUIScriptWnd) function msg_box_ui:__init(typ,show,txt,func_ok,func_cancel,...) super() self:SetWndRect (Frect():set(0,0,1024,768)) self.message_box = CUIMessageBoxEx() self:Register(self.message_box, "msg_box") self:InitCallBacks() self.message_box:InitMessageBox(typ) self.message_box:ShowDialog(show) self.message_box:SetText(txt) self.func_ok = func_ok self.func_cancel = func_cancel self.p = {...} end function msg_box_ui:__finalize() end function msg_box_ui:InitCallBacks() self:AddCallback("msg_box", ui_events.MESSAGE_BOX_OK_CLICKED, self.OnMsgOk, self) self:AddCallback("msg_box", ui_events.MESSAGE_BOX_CANCEL_CLICKED, self.OnMsgCancel, self) self:AddCallback("msg_box", ui_events.MESSAGE_BOX_YES_CLICKED, self.OnMsgOk, self) self:AddCallback("msg_box", ui_events.MESSAGE_BOX_NO_CLICKED, self.OnMsgCancel, self) end function msg_box_ui:OnMsgOk() if (self.func_ok) then self.func_ok(self,self.p) end end function msg_box_ui:OnKeyboard(dik, keyboard_action) CUIScriptWnd.OnKeyboard(self,dik,keyboard_action) if (keyboard_action == ui_events.WINDOW_KEY_PRESSED) then if (dik == DIK_keys.DIK_RETURN) then elseif (dik == DIK_keys.DIK_ESCAPE) then self:OnMsgCancel() end end return true end function msg_box_ui:OnMsgCancel() if (self.func_cancel) then self.func_cancel(self,self.p) end end ------------------------------------------------------------------- class "load_item" (CUIListBoxItem) function load_item:__init(height) super(height) self.file_name = "filename" self:SetTextColor(GetARGB(255, 170, 170, 170)) self.fn = self:GetTextItem() self.fn:SetFont(GetFontLetterica18Russian()) self.fn:SetEllipsis(true) end function load_item:__finalize() end ------------------------------------------------------------------- class "multi_choice" (CUIScriptWnd) function multi_choice:__init(action_list,...) super() self:InitControls() self:InitCallBacks() self:FillList(action_list,...) end function multi_choice:__finalize() end function multi_choice:InitControls() self:SetWndRect(Frect():set(0,0,1024,768)) self:SetAutoDelete(true) self.xml = utils_ui.get_utils_xml() local ctrl ctrl = CUIWindow() ctrl:SetAutoDelete(true) self.xml:InitWindow ("multi_choice:file_item:main",0,ctrl) self.file_item_main_sz = vector2():set(ctrl:GetWidth(),ctrl:GetHeight()) self.xml:InitWindow ("multi_choice:file_item:fn",0,ctrl) self.file_item_fn_sz = vector2():set(ctrl:GetWidth(),ctrl:GetHeight()) self.xml:InitWindow ("multi_choice:file_item:fd",0,ctrl) self.file_item_fd_sz = vector2():set(ctrl:GetWidth(),ctrl:GetHeight()) self.form = self.xml:InitStatic("multi_choice:form",self) --self.form:SetWndPos(vector2():set(device().width/4-(self.form:GetWidth()/4), device().height/2 - self.form:GetHeight())) -- List Box self.xml:InitFrame("multi_choice:form:list_frame",self.form) self.list_box = self.xml:InitListBox("multi_choice:form:list",self.form) self.list_box:ShowSelectedItem (true) self:Register (self.list_box, "list_window") -- Button OK --[[ ctrl = self.xml:Init3tButton("multi_choice:form:btn_ok",self.form) self:Register(ctrl, "button_ok") --]] end function multi_choice:InitCallBacks() self:AddCallback("button_ok",ui_events.BUTTON_CLICKED,self.OnButton_ok,self) self:AddCallback("list_window",ui_events.LIST_ITEM_CLICKED,self.OnListItemClicked,self) self:AddCallback("list_window",ui_events.WINDOW_LBUTTON_DB_CLICK,self.OnListItemDbClicked,self) end function multi_choice:FillList(action_list,...) self.list_box:RemoveAll() self.choices = {...} for i,str_id in pairs(self.choices) do self:AddItemToList(i,str_id,action_list[i]) end self:ShowDialog() end function multi_choice:OnListItemClicked() if self.list_box:GetSize()==0 then return end local item = self.list_box:GetSelectedItem() if not (item) then return end end function multi_choice:OnListItemDbClicked() if self.list_box:GetSize()==0 then return end local item = self.list_box:GetSelectedItem() if not (item) then return end if (item.func) then item.func() end if (self:IsShown()) then self:HideDialog() end end function multi_choice:OnButton_ok() end function multi_choice:OnKeyboard(dik, keyboard_action) CUIScriptWnd.OnKeyboard(self,dik,keyboard_action) if (keyboard_action == ui_events.WINDOW_KEY_PRESSED) then if (dik == DIK_keys.DIK_RETURN) then elseif (dik == DIK_keys.DIK_ESCAPE) then if (self:IsShown()) then self:HideDialog() end end end return true end function multi_choice:AddItemToList(index,str_id,func) local _itm = load_item(self.file_item_main_sz.y) local txt = index .. ". " .. game.translate_string(str_id) or "This is obviously broken if you see this" _itm:SetWndSize (self.file_item_main_sz) _itm.fn:SetWndPos(vector2():set(0,0)) _itm.fn:SetWndSize(self.file_item_fn_sz) _itm.fn:SetText(txt) _itm.func = func self.list_box:AddExistingItem(_itm) end ----------------------------------------------------------------- class "context_menu" (CUIScriptWnd) function context_menu:__init(owner,pos_override,action_list,...) super() self.owner = owner self:InitControls() self:InitCallBacks() self:FillList(action_list,...) local pos = GetCursorPosition() -- because ShowDialog moves mouse cursor to center self:ShowDialog() SetCursorPosition(pos) pos = pos_override and vector2():set(pos_override.x,pos_override.y) or GetCursorPosition() pos.x = pos.x - self.form:GetWidth()/2 pos.y = pos.y - 22 if (pos.x < 0) then pos.x = 0 end self.form:SetWndPos(pos) end function context_menu:__finalize() if (self.owner) then self.owner.disabled = false end end function context_menu:InitControls() self:SetWndRect(Frect():set(0,0,1024,768)) self:SetAutoDelete(true) self.xml = utils_ui.get_utils_xml() -- these windows are just to get width/height from xml to be use for context item local ctrl = CUIWindow() ctrl:SetAutoDelete(true) self.xml:InitWindow("context_menu:file_item:main",0,ctrl) self.file_item_main_sz = vector2():set(ctrl:GetWidth(),ctrl:GetHeight()) self.xml:InitWindow("context_menu:file_item:fn",0,ctrl) self.file_item_fn_sz = vector2():set(ctrl:GetWidth(),ctrl:GetHeight()) -- form background self.form = self.xml:InitStatic("context_menu:form",self) -- List Box self.xml:InitFrame("context_menu:form:list_frame",self.form) self.list_box = self.xml:InitListBox("context_menu:form:list",self.form) self.list_box:ShowSelectedItem(true) self:Register(self.list_box, "context_menu_list_view") end function context_menu:Update() CUIScriptWnd.Update(self) local pos = GetCursorPosition() local rect = Frect():set(0,0,0,0) self.form:GetAbsoluteRect(rect) if not (utils.pos_in_rect(pos,rect)) then if (self:IsShown()) then if (self.owner) then self.owner.disabled = false end self:HideDialog() return end end if (self.owner) then self.owner.disabled = true end end function context_menu:FillList(action_list,...) self.list_box:RemoveAll() self.choices = {...} for i,str_id in pairs(self.choices) do self:AddItemToList(i,str_id,action_list[i]) end end function context_menu:OnListItemClicked() if self.list_box:GetSize()==0 then return end local item = self.list_box:GetSelectedItem() if not (item) then return end end function context_menu:OnListItemDbClicked() if self.list_box:GetSize()==0 then return end local item = self.list_box:GetSelectedItem() if not (item) then return end if (self.owner) then self.owner.disabled = false end if (item.func) then item.func() end if (self:IsShown()) then self:HideDialog() end end function context_menu:InitCallBacks() self:AddCallback("button_ok",ui_events.BUTTON_CLICKED,self.OnButton_ok,self) self:AddCallback("context_menu_list_view",ui_events.LIST_ITEM_CLICKED,self.OnListItemDbClicked,self) --self:AddCallback("context_menu_list_view",ui_events.WINDOW_LBUTTON_DB_CLICK,self.OnListItemDbClicked,self) end function context_menu:AddItemToList(index,str_id,func) local _itm = context_item() _itm:SetWndSize(self.file_item_main_sz) _itm.textControl:SetWndPos(vector2():set(0,0)) _itm.textControl:SetWndSize(self.file_item_fn_sz) _itm.textControl:SetText(game.translate_string(str_id)) _itm.func = func self.list_box:AddExistingItem(_itm) end function context_menu:OnKeyboard(dik, keyboard_action) CUIScriptWnd.OnKeyboard(self,dik,keyboard_action) if (keyboard_action == ui_events.WINDOW_KEY_PRESSED) then if (dik == DIK_keys.DIK_RETURN) then elseif (dik == DIK_keys.DIK_ESCAPE) then if (self:IsShown()) then if (self.owner) then self.owner.disabled = false end self:HideDialog() end end end return true end ----------------------------------------------------------------- -- same as context_menu, different look and adjustable frame class "context_props" (CUIScriptWnd) function context_props:__init(owner) super() self.owner = owner self:InitControls() self:InitCallBacks() end function context_props:__finalize() if (self.owner) then --self.owner.disabled = false end end function context_props:InitControls() self:SetWndRect(Frect():set(0,0,1024,768)) self:SetAutoDelete(true) self.xml = utils_ui.get_utils_xml() local xml = self.xml -- these windows are just to get width/height from xml to be use for context item local ctrl = CUIWindow() ctrl:SetAutoDelete(true) xml:InitWindow("context_props:file_item:main",0,ctrl) self.ctrl_h = ctrl:GetHeight() self.file_item_main_sz = vector2():set(ctrl:GetWidth(),ctrl:GetHeight()) xml:InitWindow("context_props:file_item:fn",0,ctrl) self.file_item_fn_sz = vector2():set(ctrl:GetWidth(),ctrl:GetHeight()) -- form background self.form = xml:InitStatic("context_props:form",self) -- List Box self.frame = xml:InitFrame("context_props:form:list_frame",self.form) self.list_box = xml:InitListBox("context_props:form:list",self.form) self.list_box:ShowSelectedItem(true) self:Register(self.list_box, "context_props_list_view") end function context_props:Update() CUIScriptWnd.Update(self) --[[ local pos = GetCursorPosition() local rect = Frect():set(0,0,0,0) self.form:GetAbsoluteRect(rect) if not (utils_data.pos_in_rect(pos,rect)) then if (self:IsShown()) then self:OnHide() return end end if (self.owner) then self.owner.disabled = true end --]] for i=0,self.list_box:GetSize() do local _itm = self.list_box:GetItemByIndex(i) if _itm and _itm:IsCursorOverWindow() then local pos = _itm.textControl:GetWndPos() self.highlight:SetWndPos( vector2():set( pos.x + 10 , pos.y + 10 ) ) self.highlight:SetWndSize(vector2():set( _itm.textControl:GetWidth() , _itm.textControl:GetHeight() )) self.highlight:Show(true) return end end self.highlight:Show(false) end function context_props:Reset(pos_override,action_list,name_list,params_list) local num = #name_list self.H = 40 self.W = 58 if num * self.ctrl_h > 40 then self.H = num * self.ctrl_h end -- Reset list self:FillList(action_list,name_list,params_list) -- Cursor local pos = GetCursorPosition() -- because ShowDialog moves mouse cursor to center self:ShowDialog() SetCursorPosition(pos) pos = pos_override and vector2():set(pos_override.x,pos_override.y) or GetCursorPosition() pos.x = pos.x - self.form:GetWidth() --/2 --pos.y = pos.y - 22 if (pos.x < 0) then pos.x = 0 end self.form:SetWndPos(pos) end function context_props:FillList(action_list,name_list,params_list) self.list_box:RemoveAll() for i,str_id in pairs(name_list) do self:AddItemToList(i,str_id,action_list[i],params_list and params_list[i]) end local sz = vector2():set(self.W , self.H) self.form:SetWndSize(sz) self.frame:SetWndSize(sz) self.list_box:SetWndSize(sz) self.highlight = self.xml:InitStatic("context_props:highlight",self.form) self.highlight:Show(false) end function context_props:OnListItemClicked() if self.list_box:GetSize()==0 then return end local item = self.list_box:GetSelectedItem() if not (item) then return end end function context_props:OnListItemDbClicked() if self.list_box:GetSize()==0 then return end local item = self.list_box:GetSelectedItem() if not (item) then return end if (item.func) then item.func(self.owner, item.params and unpack(item.params)) end if (self:IsShown()) then self:OnHide() end end function context_props:InitCallBacks() self:AddCallback("button_ok",ui_events.BUTTON_CLICKED,self.OnButton_ok,self) self:AddCallback("context_props_list_view",ui_events.LIST_ITEM_CLICKED,self.OnListItemDbClicked,self) --self:AddCallback("context_props_list_view",ui_events.WINDOW_LBUTTON_DB_CLICK,self.OnListItemDbClicked,self) end function context_props:AddItemToList(index,str_id,func,params) local _itm = context_item() _itm:SetWndSize(self.file_item_main_sz) _itm.textControl:SetWndPos(vector2():set(0,0)) _itm.textControl:SetWndSize(self.file_item_fn_sz) _itm.textControl:SetText(game.translate_string(str_id)) _itm.textControl:AdjustWidthToText() local h = self.file_item_fn_sz.y local w = _itm.textControl:GetWidth() if w > self.W then self.W = w end local sz = vector2():set( w + 10 , h ) _itm.textControl:SetWndSize(sz) _itm:SetWndSize(sz) _itm.func = func _itm.params = params self.list_box:AddExistingItem(_itm) end function context_props:OnKeyboard(dik, keyboard_action) CUIScriptWnd.OnKeyboard(self,dik,keyboard_action) if (keyboard_action == ui_events.WINDOW_KEY_PRESSED) then if (dik == DIK_keys.MOUSE_1) or (dik == DIK_keys.MOUSE_2) then if (not self.frame:IsCursorOverWindow()) then if (self:IsShown()) then self:OnHide() end end elseif (dik == DIK_keys.DIK_ESCAPE) then if (self:IsShown()) then self:OnHide() end end end return true end function context_props:OnHide() if (self.owner) then --self.owner.disabled = false end self:HideDialog() end ---- class "context_item" (CUIListBoxItem) function context_item:__init() super() self:SetTextColor(GetARGB(255, 170, 170, 170)) self.textControl = self:GetTextItem() self.textControl:SetFont(GetFontLetterica16Russian()) self.textControl:SetEllipsis(true) end function context_item:__finalize() end